//
//  ProductRowCell.m
//  IGOtest
//
//  Created by administrator on 14-2-22.
//  Copyright (c) 2014年 administrator. All rights reserved.
//

#define kTagStart 100

#import "ProductRowCell.h"
#import "Product.h"
#import "MyButton.h"

@implementation ProductRowCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withCellRow:(int)num
{
    self = [self initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        for (int i = 0; i<kColumn; i++) {
            MyButton *btn = [MyButton buttonWithType:UIButtonTypeCustom];
            // 设置格子的宽高和位置
            CGFloat btnWidth = self.frame.size.width / kColumn;
            CGFloat btnX = i * btnWidth;
            CGFloat btnY = 0;
            CGFloat btnHeight = kCellHeight;
            btn.frame = CGRectMake(btnX, btnY, btnWidth, btnHeight);
            // 设置格子的tag
            btn.tag = i + kTagStart+ 3 * num;
            // 添加格子到cell的contentView中
            [self.contentView addSubview:btn];
        }
    }
    return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
//    if (self) {
//        for (int i = 0; i<kColumn; i++) {
//            MyButton *btn = [MyButton buttonWithType:UIButtonTypeCustom];
//            // 设置格子的宽高和位置
//            CGFloat btnWidth = self.frame.size.width / kColumn;
//            CGFloat btnX = i * btnWidth;
//            CGFloat btnY = 0;
//            CGFloat btnHeight = kCellHeight;
//            btn.frame = CGRectMake(btnX, btnY, btnWidth, btnHeight);
//            // 设置格子的tag
//            btn.tag = i + kTagStart ;
//            // 添加格子到cell的contentView中
//            [self.contentView addSubview:btn];
//        }
//    }
    return self;
}

#pragma mark 设置所有格子的数据（个数可能是1、2、3、...kColumn）
- (void)setRowProducts:(NSArray *)rowProducts withCellRow:(int)num
{
    // 遍历所有的格子
    for (int i = 0; i<kColumn; i++) {
        int tag = i + kTagStart + 3 * num;
        MyButton *btn = (MyButton *)[self.contentView viewWithTag:tag];
        if (i>rowProducts.count - 1) {
            
            btn.hidden = YES;
            
        } else {
            Product *theproduct = rowProducts[i];
            [btn setTitle:theproduct.name forState:UIControlStateNormal];
            // 设置格子显示的图片
//            UIImage *image = [UIImage imageNamed:theproduct.icon];
            // 设置内部UIImageView的图片
            [btn setImage:theproduct.icon forState:UIControlStateNormal];
            // 设置背景图片
//            NSLog(@"%@",image);
//            [btn setBackgroundImage:image forState:UIControlStateNormal];
        
            btn.hidden = NO; // 显示格子
        }
    }
}
@end